package util;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by jifeng on 2017/1/5 0005.创建订单号
 */
public class CreateOrder {

    /**
     * 获取扩展名
     *
     * @param file
     * @return
     */
    public static String getExtensionName(File file) {
        return file.getName().substring(file.getName().lastIndexOf(".") + 1, file.getName().length());
    }


    /**
     * 创建 ordercode
     *
     * @return
     */
    public static String formateCode(long id1, long id2) {
        return formatDateTimeWithMiliSecond(new Date()) + strFrom(id1, 5) + strFrom(id2, 5);
    }

    public static String formatDateTimeWithMiliSecond(Date date) {
        DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return sdf.format(date);
    }

    public static String strFrom(long code, int width) {
        String str = "" + code;
        while (str.length() < width) {
            str = "0" + str;
        }
        if (str.length() > width) {
            str = str.substring(str.length() - width);
        }
        return str;
    }

    /**
     * 获取当前日期是星期几<br>
     *
     * @param dt
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }
}
